<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="爱肖彤真是太好了"><meta name="copyright" content="爱肖彤真是太好了"><meta name="generator" content="Hexo 5.1.1"><meta name="theme" content="hexo-theme-yun"><title>Rabbitmq、Rpc | 工藤旧二の博客</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.19/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/task/yun.ico"><link rel="mask-icon" href="/task/yun.ico" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/task/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/task/js/utils.js" as="script"><link rel="preload" href="/task/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/task/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/task/css/hexo-theme-yun.css"><link rel="alternate" href="/task/atom.xml" title="工藤旧二の博客" type="application/atom+xml"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/task/","title":"工藤旧二の博客","version":"0.9.7","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/task/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="消息队列介绍：http:&#x2F;&#x2F;mrw.so&#x2F;6m21VY 1 安装1 原生安装     -安装扩展epel源     -yum -y install erlang     -yum -y install rabbitmq-server     -systemctl start rabbitmq-server  2 docker拉取     -docker pull rabbitmq:manageme">
<meta property="og:type" content="article">
<meta property="og:title" content="Rabbitmq、Rpc">
<meta property="og:url" content="http://huang_zhao.gitee.io/task/2020/03/24/%E5%85%B6%E4%BB%96/Rabbitmq%EF%BC%8CRpc/index.html">
<meta property="og:site_name" content="工藤旧二の博客">
<meta property="og:description" content="消息队列介绍：http:&#x2F;&#x2F;mrw.so&#x2F;6m21VY 1 安装1 原生安装     -安装扩展epel源     -yum -y install erlang     -yum -y install rabbitmq-server     -systemctl start rabbitmq-server  2 docker拉取     -docker pull rabbitmq:manageme">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-24T12:30:17.000Z">
<meta property="article:modified_time" content="2020-09-09T11:19:32.000Z">
<meta property="article:author" content="爱肖彤真是太好了">
<meta property="article:tag" content="Rabbitmq">
<meta property="article:tag" content="Rpc">
<meta property="article:tag" content="消息队列">
<meta name="twitter:card" content="summary"><script src="/task/js/ui/mode.js"></script><link rel="stylesheet" href="/task/css/prism.css" type="text/css"></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/task/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/task/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/task/about/" title="爱肖彤真是太好了"><img width="96" loading="lazy" src="/task/Yun.png" alt="爱肖彤真是太好了"></a><div class="site-author-name"><a href="/task/about/">爱肖彤真是太好了</a></div><a class="site-name" href="/task/about/site.html">工藤旧二の博客</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/task/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/task/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">57</span></a></div><div class="site-state-item"><a href="/task/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/task/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">50</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=552858287" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/15081363" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/task/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%AE%89%E8%A3%85"><span class="toc-text">1 安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">2 基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-%E6%B6%88%E6%81%AF%E5%AE%89%E5%85%A8%E2%98%9Eack"><span class="toc-text">2.1 消息安全☞ack</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-%E6%B6%88%E6%81%AF%E5%AE%89%E5%85%A8%E2%98%9Edurable%E6%8C%81%E4%B9%85%E5%8C%96"><span class="toc-text">2.2 消息安全☞durable持久化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-%E9%97%B2%E7%BD%AE%E6%B6%88%E8%B4%B9"><span class="toc-text">2.3 闲置消费</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-4-%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85"><span class="toc-text">2.4 发布订阅</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E6%89%A9%E2%98%9ERouting-%E6%8C%89%E5%85%B3%E9%94%AE%E5%AD%97%E5%8C%B9%E9%85%8D"><span class="toc-text">2.5 发布订阅扩☞Routing(按关键字匹配)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85%E9%AB%98%E7%BA%A7%E2%98%9ETopic-%E6%8C%89%E5%85%B3%E9%94%AE%E5%AD%97%E6%A8%A1%E7%B3%8A%E5%8C%B9%E9%85%8D"><span class="toc-text">2.6 发布订阅高级☞Topic(按关键字模糊匹配)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-rpc%E8%BF%9C%E7%A8%8B%E8%BF%87%E7%A8%8B%E8%B0%83%E7%94%A8"><span class="toc-text">3 rpc远程过程调用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-rabbitmq%E5%AE%9E%E7%8E%B0rpc"><span class="toc-text">3.1 rabbitmq实现rpc</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-SimpleXMLRPCServer%E5%AE%9E%E7%8E%B0rpc"><span class="toc-text">3.2 SimpleXMLRPCServer实现rpc</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-ZeroRPC%E5%AE%9E%E7%8E%B0rpc"><span class="toc-text">3.3 ZeroRPC实现rpc</span></a></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://huang_zhao.gitee.io/task/task/2020/03/24/%E5%85%B6%E4%BB%96/Rabbitmq%EF%BC%8CRpc/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="爱肖彤真是太好了"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="工藤旧二の博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Rabbitmq、Rpc</h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2020-03-24 20:30:17" itemprop="dateCreated datePublished" datetime="2020-03-24T20:30:17+08:00">2020-03-24</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2020-09-09 19:19:32" itemprop="dateModified" datetime="2020-09-09T19:19:32+08:00">2020-09-09</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">2.1k</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">10m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/task/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">技术分享</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/task/tags/Rabbitmq/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Rabbitmq</span></a><a class="tag" href="/task/tags/Rpc/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Rpc</span></a><a class="tag" href="/task/tags/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">消息队列</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><p>消息队列介绍：<strong><a target="_blank" rel="noopener" href="http://mrw.so/6m21VY">http://mrw.so/6m21VY</a></strong></p>
<h3 id="1-安装"><a href="#1-安装" class="headerlink" title="1 安装"></a>1 安装</h3><pre class=" language-python"><code class="language-python"><span class="token number">1</span> 原生安装
    <span class="token operator">-</span>安装扩展epel源
    <span class="token operator">-</span>yum <span class="token operator">-</span>y install erlang
    <span class="token operator">-</span>yum <span class="token operator">-</span>y install rabbitmq<span class="token operator">-</span>server
    <span class="token operator">-</span>systemctl start rabbitmq<span class="token operator">-</span>server

<span class="token number">2</span> docker拉取
    <span class="token operator">-</span>docker pull rabbitmq<span class="token punctuation">:</span>management（自动开启了web管理界面）
    <span class="token operator">-</span>docker run <span class="token operator">-</span>di <span class="token operator">-</span><span class="token operator">-</span>name rabbitmq <span class="token operator">-</span>e RABBITMQ_DEFAULT_USER<span class="token operator">=</span>admin <span class="token operator">-</span>e RABBITMQ_DEFAULT_PASS<span class="token operator">=</span>admin <span class="token operator">-</span>p <span class="token number">15672</span><span class="token punctuation">:</span><span class="token number">15672</span> <span class="token operator">-</span>p <span class="token number">5672</span><span class="token punctuation">:</span><span class="token number">5672</span> rabbitmq<span class="token punctuation">:</span>management

<span class="token number">3</span> <span class="token number">5672</span>：是rabbitmq的默认端口
  <span class="token number">15672</span>：web管理界面的端口


<span class="token number">4</span> 创建用户
rabbitmqctl add_user lqz <span class="token number">123</span>
<span class="token number">5</span> 分配权限
rabbitmqctl set_user_tags lqz administrator
rabbitmqctl set_permissions <span class="token operator">-</span>p <span class="token string">"/"</span> lqz <span class="token string">".*"</span> <span class="token string">".*"</span> <span class="token string">".*"</span></code></pre>
<h3 id="2-基本使用"><a href="#2-基本使用" class="headerlink" title="2 基本使用"></a>2 基本使用</h3><h4 id="2-1-消息安全☞ack"><a href="#2-1-消息安全☞ack" class="headerlink" title="2.1 消息安全☞ack"></a>2.1 消息安全☞ack</h4><p>是否响应数据已经被接受</p>
<p>就是消费者中接受数据的时候配置一个参数auto_ack=False，就会导致拿到数据了，但是数据还在队列中</p>
<p><strong>消费者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment" spellcheck="true"># 创建一个用户对象</span>
    credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"admin"</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 创建一个链接对象</span>
    connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'121.36.98.49'</span><span class="token punctuation">,</span> credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 创建操作对象</span>
    channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 创建一个队列（默认是在生产者中创建，为了防止消费者先启动，多写也不会重复创建）</span>
    channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" [x] Received %r"</span> <span class="token operator">%</span> body<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 等到hello队列中出现数据，如果出现则执行callback</span>
    channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">,</span> on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span> auto_ack<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 一直启动等待接收消息</span>
    channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>

    main<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><strong>生产者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika
<span class="token comment" spellcheck="true"># 拿到连接对象</span>
<span class="token comment" spellcheck="true"># connection = pika.BlockingConnection(pika.ConnectionParameters('101.133.225.166'))</span>
<span class="token comment" spellcheck="true"># 有用户名密码的情况</span>
credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'121.36.98.49'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 拿到channel对象</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 声明一个队列</span>
channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 指定队列名字</span>

<span class="token comment" spellcheck="true"># 生产者向hello队列中放一条消息"lqz js nb"</span>
channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>
                      routing_key<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">,</span>
                      body<span class="token operator">=</span><span class="token string">'lqz js nb'</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" Sent 'Hello World!'"</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 关闭连接</span>
connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="2-2-消息安全☞durable持久化"><a href="#2-2-消息安全☞durable持久化" class="headerlink" title="2.2 消息安全☞durable持久化"></a>2.2 消息安全☞durable持久化</h4><p>durable持久化消费者不需要动，只要在生产者产生数据的时候配置一个参数即可</p>
<p>注意：配置持久化必须是新的队列</p>
<p><strong>生产者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 声明一个队列(创建一个队列),durable=True支持持久化，队列必须是新的才可以</span>
channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'lqz1'</span><span class="token punctuation">,</span>durable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>
                      routing_key<span class="token operator">=</span><span class="token string">'lqz1'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true"># 消息队列名称</span>
                      body<span class="token operator">=</span><span class="token string">'111'</span><span class="token punctuation">,</span>
                      properties<span class="token operator">=</span>pika<span class="token punctuation">.</span>BasicProperties<span class="token punctuation">(</span>
                          delivery_mode<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>  <span class="token comment" spellcheck="true"># make message persistent,消息也持久化</span>
                      <span class="token punctuation">)</span>
                      <span class="token punctuation">)</span>
connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="2-3-闲置消费"><a href="#2-3-闲置消费" class="headerlink" title="2.3 闲置消费"></a>2.3 闲置消费</h4><p>默认情况下，如果有多个消费者，是按照顺序第一个消息给第一个消费者，第二个消息给第二个消费者</p>
<p>但是可能第一个消息的消费者处理消息很耗时，一直没结束，就可以让第二个消费者优先获得闲置的消息</p>
<p>就是在消费者接受数据前配置一个参数</p>
<p><strong>消费者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 声明一个队列(创建一个队列)</span>
<span class="token comment" spellcheck="true"># channel.queue_declare(queue='lqz123')</span>

<span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"消费者接受到了任务: %r"</span> <span class="token operator">%</span> body<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true"># 通知服务端，消息取走了，如果auto_ack=False，不加下面，消息会一直存在</span>
    ch<span class="token punctuation">.</span>basic_ack<span class="token punctuation">(</span>delivery_tag<span class="token operator">=</span>method<span class="token punctuation">.</span>delivery_tag<span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_qos<span class="token punctuation">(</span>prefetch_count<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">#####就只有这一句话 谁闲置谁获取，没必要按照顺序一个一个来</span>
channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'lqz123'</span><span class="token punctuation">,</span>on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span>auto_ack<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="2-4-发布订阅"><a href="#2-4-发布订阅" class="headerlink" title="2.4 发布订阅"></a>2.4 发布订阅</h4><p>发布订阅的本质是观察者模式，发布者发布了内容，订阅者只要订阅了发布者就会接受发布者推送的内容</p>
<p><strong>发布者</strong></p>
<p>与上面生产者模式的不同，这里发布者是通过一个中间者，所有绑定了这个中间者的订阅者绑定的队列都会接受到这个推送</p>
<p>就像a,b,c（订阅者）都关注了一个公众号（发布者），只要公众号发了消息，都会由微信的服务器（中间者）转发这个消息到a,b,c的账号（队列）上</p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika
credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m1'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'fanout'</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m1'</span><span class="token punctuation">,</span>
                      routing_key<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>
                      body<span class="token operator">=</span><span class="token string">'lqz nb'</span><span class="token punctuation">)</span>

connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><strong>订阅者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># exchange='m1',exchange(秘书)的名称</span>
<span class="token comment" spellcheck="true"># exchange_type='fanout' , 秘书工作方式将消息发送给所有的队列</span>
channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m1'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'fanout'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 随机生成一个队列</span>
result <span class="token operator">=</span> channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>exclusive<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
queue_name <span class="token operator">=</span> result<span class="token punctuation">.</span>method<span class="token punctuation">.</span>queue
<span class="token keyword">print</span><span class="token punctuation">(</span>queue_name<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 让exchange和queque进行绑定.</span>
channel<span class="token punctuation">.</span>queue_bind<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m1'</span><span class="token punctuation">,</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"消费者接受到了任务: %r"</span> <span class="token operator">%</span> body<span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span>auto_ack<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="2-5-发布订阅扩☞Routing-按关键字匹配"><a href="#2-5-发布订阅扩☞Routing-按关键字匹配" class="headerlink" title="2.5 发布订阅扩☞Routing(按关键字匹配)"></a>2.5 发布订阅扩☞Routing(按关键字匹配)</h4><p>发布订阅接受推送的条件默认是只有订阅者能接受，routing限定了订阅者必须要有绑定这个关键字</p>
<p><strong>发布者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika
credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m2'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'direct'</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m2'</span><span class="token punctuation">,</span>
                      routing_key<span class="token operator">=</span><span class="token string">'bnb'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true"># 多个关键字，指定routing_key</span>
                      body<span class="token operator">=</span><span class="token string">'lqz nb'</span><span class="token punctuation">)</span>

connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><strong>订阅者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># exchange='m1',exchange(秘书)的名称</span>
<span class="token comment" spellcheck="true"># exchange_type='direct' , 秘书工作方式将消息发送给不同的关键字</span>
channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m2'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'direct'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 随机生成一个队列</span>
result <span class="token operator">=</span> channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>exclusive<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
queue_name <span class="token operator">=</span> result<span class="token punctuation">.</span>method<span class="token punctuation">.</span>queue
<span class="token keyword">print</span><span class="token punctuation">(</span>queue_name<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 让exchange和queque进行绑定. 可以有多个</span>
channel<span class="token punctuation">.</span>queue_bind<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m2'</span><span class="token punctuation">,</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>routing_key<span class="token operator">=</span><span class="token string">'nb'</span><span class="token punctuation">)</span>
channel<span class="token punctuation">.</span>queue_bind<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m2'</span><span class="token punctuation">,</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>routing_key<span class="token operator">=</span><span class="token string">'bnb'</span><span class="token punctuation">)</span>


<span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"消费者接受到了任务: %r"</span> <span class="token operator">%</span> body<span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span>auto_ack<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="2-6-发布订阅高级☞Topic-按关键字模糊匹配"><a href="#2-6-发布订阅高级☞Topic-按关键字模糊匹配" class="headerlink" title="2.6 发布订阅高级☞Topic(按关键字模糊匹配)"></a>2.6 发布订阅高级☞Topic(按关键字模糊匹配)</h4><p><strong>发布者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika
credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m3'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'topic'</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m3'</span><span class="token punctuation">,</span>
                      <span class="token comment" spellcheck="true"># routing_key='lqz.handsome', #都能收到</span>
                      routing_key<span class="token operator">=</span><span class="token string">'lqz.handsome.xx'</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">#只有lqz.#能收到</span>
                      body<span class="token operator">=</span><span class="token string">'lqz nb'</span><span class="token punctuation">)</span>

connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p>*只能加一个单词</p>
<p>#可以加任意单词字符</p>
<p><strong>订阅者</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span>credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># exchange='m1',exchange(秘书)的名称</span>
<span class="token comment" spellcheck="true"># exchange_type='direct' , 秘书工作方式将消息发送给不同的关键字</span>
channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m3'</span><span class="token punctuation">,</span>exchange_type<span class="token operator">=</span><span class="token string">'topic'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 随机生成一个队列</span>
result <span class="token operator">=</span> channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>exclusive<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
queue_name <span class="token operator">=</span> result<span class="token punctuation">.</span>method<span class="token punctuation">.</span>queue
<span class="token keyword">print</span><span class="token punctuation">(</span>queue_name<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 让exchange和queque进行绑定.</span>
channel<span class="token punctuation">.</span>queue_bind<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">'m3'</span><span class="token punctuation">,</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>routing_key<span class="token operator">=</span><span class="token string">'lqz.#'</span><span class="token punctuation">)</span>



<span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"消费者接受到了任务: %r"</span> <span class="token operator">%</span> body<span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span>queue_name<span class="token punctuation">,</span>on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span>auto_ack<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h3 id="3-rpc远程过程调用"><a href="#3-rpc远程过程调用" class="headerlink" title="3 rpc远程过程调用"></a>3 rpc远程过程调用</h3><p>是微服务的一种概念，通过http请求或者socket请求去服务端调用函数或者类的方法</p>
<h4 id="3-1-rabbitmq实现rpc"><a href="#3-1-rabbitmq实现rpc" class="headerlink" title="3.1 rabbitmq实现rpc"></a>3.1 rabbitmq实现rpc</h4><p><strong>服务端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika

credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"admin"</span><span class="token punctuation">)</span>
connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span> credentials<span class="token operator">=</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>


channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'rpc_queue'</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">fib</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> n <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token number">0</span>
    <span class="token keyword">elif</span> n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> <span class="token number">1</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> fib<span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> fib<span class="token punctuation">(</span>n <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">on_request</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> props<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
    n <span class="token operator">=</span> int<span class="token punctuation">(</span>body<span class="token punctuation">)</span>

    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" [.] fib(%s)"</span> <span class="token operator">%</span> n<span class="token punctuation">)</span>
    response <span class="token operator">=</span> fib<span class="token punctuation">(</span>n<span class="token punctuation">)</span>

    ch<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>
                     routing_key<span class="token operator">=</span>props<span class="token punctuation">.</span>reply_to<span class="token punctuation">,</span>
                     properties<span class="token operator">=</span>pika<span class="token punctuation">.</span>BasicProperties<span class="token punctuation">(</span>correlation_id <span class="token operator">=</span> \
                                                         props<span class="token punctuation">.</span>correlation_id<span class="token punctuation">)</span><span class="token punctuation">,</span>
                     body<span class="token operator">=</span>str<span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">)</span>
    ch<span class="token punctuation">.</span>basic_ack<span class="token punctuation">(</span>delivery_tag<span class="token operator">=</span>method<span class="token punctuation">.</span>delivery_tag<span class="token punctuation">)</span>

channel<span class="token punctuation">.</span>basic_qos<span class="token punctuation">(</span>prefetch_count<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'rpc_queue'</span><span class="token punctuation">,</span> on_message_callback<span class="token operator">=</span>on_request<span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" [x] Awaiting RPC requests"</span><span class="token punctuation">)</span>
channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><strong>客户端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pika
<span class="token keyword">import</span> uuid

<span class="token keyword">class</span> <span class="token class-name">FibonacciRpcClient</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>

    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>

        self<span class="token punctuation">.</span>credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">,</span> <span class="token string">"admin"</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'101.133.225.166'</span><span class="token punctuation">,</span> credentials<span class="token operator">=</span>self<span class="token punctuation">.</span>credentials<span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>channel <span class="token operator">=</span> self<span class="token punctuation">.</span>connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span>

        result <span class="token operator">=</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span> exclusive<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>callback_queue <span class="token operator">=</span> result<span class="token punctuation">.</span>method<span class="token punctuation">.</span>queue

        self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>
            queue<span class="token operator">=</span>self<span class="token punctuation">.</span>callback_queue<span class="token punctuation">,</span>
            on_message_callback<span class="token operator">=</span>self<span class="token punctuation">.</span>on_response<span class="token punctuation">,</span>
            auto_ack<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">on_response</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> ch<span class="token punctuation">,</span> method<span class="token punctuation">,</span> props<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>corr_id <span class="token operator">==</span> props<span class="token punctuation">.</span>correlation_id<span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>response <span class="token operator">=</span> body

    <span class="token keyword">def</span> <span class="token function">call</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>response <span class="token operator">=</span> None
        self<span class="token punctuation">.</span>corr_id <span class="token operator">=</span> str<span class="token punctuation">(</span>uuid<span class="token punctuation">.</span>uuid4<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>
            exchange<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span>
            routing_key<span class="token operator">=</span><span class="token string">'rpc_queue'</span><span class="token punctuation">,</span>
            properties<span class="token operator">=</span>pika<span class="token punctuation">.</span>BasicProperties<span class="token punctuation">(</span>
                reply_to<span class="token operator">=</span>self<span class="token punctuation">.</span>callback_queue<span class="token punctuation">,</span>
                correlation_id<span class="token operator">=</span>self<span class="token punctuation">.</span>corr_id<span class="token punctuation">,</span>
            <span class="token punctuation">)</span><span class="token punctuation">,</span>
            body<span class="token operator">=</span>str<span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">while</span> self<span class="token punctuation">.</span>response <span class="token keyword">is</span> None<span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>connection<span class="token punctuation">.</span>process_data_events<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> int<span class="token punctuation">(</span>self<span class="token punctuation">.</span>response<span class="token punctuation">)</span>


fibonacci_rpc <span class="token operator">=</span> FibonacciRpcClient<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" [x] Requesting fib(30)"</span><span class="token punctuation">)</span>
response <span class="token operator">=</span> fibonacci_rpc<span class="token punctuation">.</span>call<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true"># 外界看上去，就像调用本地的call()函数一样</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">" [.] Got %r"</span> <span class="token operator">%</span> response<span class="token punctuation">)</span></code></pre>
<h4 id="3-2-SimpleXMLRPCServer实现rpc"><a href="#3-2-SimpleXMLRPCServer实现rpc" class="headerlink" title="3.2 SimpleXMLRPCServer实现rpc"></a>3.2 SimpleXMLRPCServer实现rpc</h4><p><strong>服务端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> xmlrpc<span class="token punctuation">.</span>server <span class="token keyword">import</span> SimpleXMLRPCServer
<span class="token keyword">class</span> <span class="token class-name">RPCServer</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>

    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span>RPCServer<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>send_data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'server:'</span><span class="token operator">+</span>str<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span> i <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
        self<span class="token punctuation">.</span>recv_data <span class="token operator">=</span> None

    <span class="token keyword">def</span> <span class="token function">getObj</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'get data'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>send_data

    <span class="token keyword">def</span> <span class="token function">sendObj</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> data<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'send data'</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>recv_data <span class="token operator">=</span> data
        <span class="token keyword">print</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>recv_data<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># SimpleXMLRPCServer</span>
server <span class="token operator">=</span> SimpleXMLRPCServer<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'localhost'</span><span class="token punctuation">,</span><span class="token number">4242</span><span class="token punctuation">)</span><span class="token punctuation">,</span> allow_none<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
server<span class="token punctuation">.</span>register_introspection_functions<span class="token punctuation">(</span><span class="token punctuation">)</span>
server<span class="token punctuation">.</span>register_instance<span class="token punctuation">(</span>RPCServer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
server<span class="token punctuation">.</span>serve_forever<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><strong>客户端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> time
<span class="token keyword">from</span> xmlrpc<span class="token punctuation">.</span>client <span class="token keyword">import</span> ServerProxy

<span class="token comment" spellcheck="true"># SimpleXMLRPCServer</span>
<span class="token keyword">def</span> <span class="token function">xmlrpc_client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'xmlrpc client'</span><span class="token punctuation">)</span>
    c <span class="token operator">=</span> ServerProxy<span class="token punctuation">(</span><span class="token string">'http://localhost:4242'</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'client:'</span><span class="token operator">+</span>str<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span> i <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
    start <span class="token operator">=</span> time<span class="token punctuation">.</span>clock<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        a<span class="token operator">=</span>c<span class="token punctuation">.</span>getObj<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        c<span class="token punctuation">.</span>sendObj<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'xmlrpc total time %s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>time<span class="token punctuation">.</span>clock<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> start<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    xmlrpc_client<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h4 id="3-3-ZeroRPC实现rpc"><a href="#3-3-ZeroRPC实现rpc" class="headerlink" title="3.3 ZeroRPC实现rpc"></a>3.3 ZeroRPC实现rpc</h4><p><strong>服务端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> zerorpc

<span class="token keyword">class</span> <span class="token class-name">RPCServer</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>

    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        super<span class="token punctuation">(</span>RPCServer<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>send_data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'server:'</span><span class="token operator">+</span>str<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span> i <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
        self<span class="token punctuation">.</span>recv_data <span class="token operator">=</span> None

    <span class="token keyword">def</span> <span class="token function">getObj</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'get data'</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span>send_data

    <span class="token keyword">def</span> <span class="token function">sendObj</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> data<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'send data'</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>recv_data <span class="token operator">=</span> data
        <span class="token keyword">print</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>recv_data<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># zerorpc</span>
s <span class="token operator">=</span> zerorpc<span class="token punctuation">.</span>Server<span class="token punctuation">(</span>RPCServer<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span>bind<span class="token punctuation">(</span><span class="token string">'tcp://0.0.0.0:4243'</span><span class="token punctuation">)</span>
s<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>
<p><strong>客户端</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> zerorpc
<span class="token keyword">import</span> time
<span class="token comment" spellcheck="true"># zerorpc</span>
<span class="token keyword">def</span> <span class="token function">zerorpc_client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'zerorpc client'</span><span class="token punctuation">)</span>
    c <span class="token operator">=</span> zerorpc<span class="token punctuation">.</span>Client<span class="token punctuation">(</span><span class="token punctuation">)</span>
    c<span class="token punctuation">.</span>connect<span class="token punctuation">(</span><span class="token string">'tcp://127.0.0.1:4243'</span><span class="token punctuation">)</span>
    data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'client:'</span><span class="token operator">+</span>str<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span> i <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
    start <span class="token operator">=</span> time<span class="token punctuation">.</span>clock<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        a<span class="token operator">=</span>c<span class="token punctuation">.</span>getObj<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        c<span class="token punctuation">.</span>sendObj<span class="token punctuation">(</span>data<span class="token punctuation">)</span>

    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'total time %s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>time<span class="token punctuation">.</span>clock<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> start<span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    zerorpc_client<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="打赏" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">点我就给你看点有意思的</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg" alt="QQ 支付" title="QQ 支付"></a><div><span style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg" alt="微信支付" title="微信支付"></a><div><span style="color:#2DC100"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-pay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>爱肖彤真是太好了</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="http://huang_zhao.gitee.io/task/2020/03/24/%E5%85%B6%E4%BB%96/Rabbitmq%EF%BC%8CRpc/" title="Rabbitmq、Rpc">http://huang_zhao.gitee.io/task/2020/03/24/%E5%85%B6%E4%BB%96/Rabbitmq%EF%BC%8CRpc/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/task/2020/04/21/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%8C%E4%B8%BB%E4%BB%8E%EF%BC%8C%E5%93%A8%E5%85%B5%EF%BC%8C%E9%9B%86%E7%BE%A4/" rel="prev" title="Redis持久化，主从，哨兵，集群"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">Redis持久化，主从，哨兵，集群</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/task/2020/03/24/%E5%85%B6%E4%BB%96/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/" rel="next" title="消息队列"><span class="post-nav-text">消息队列</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 爱肖彤真是太好了</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.1.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.7</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2019-04-12T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/task/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/task/js/utils.js"></script><script defer src="/task/js/hexo-theme-yun.js"></script></body></html>